<?php
namespace Admin\Controller\Lunbo;
use Admin\Controller\Auto\AutoController;;
class LunboController extends AutoController {
	public function add(){
		$this->display('/lunbo/add');
	}
	public function insert(){
		// echo '<pre>';
		// var_dump(I('post.'));
		// var_dump($_FILES);exit;
		
		$m=M('lunbo');
		$m->create(I('post.'));
		$m->pic=self::uploadPic('/upload/','./public/admin/lunbo');
		// var_dump($m->pic);exit;
		if($m->add()){
			if(I('post.auth')==0){
				echo '<script>alert("添加成功");window.location.href="/admin/lunbo/lunbo/shangcheng"</script>';
			}else{
				echo '<script>alert("添加成功");window.location.href="/admin/lunbo/lunbo/Blog"</script>';
			}
		}else{
			echo '<script>alert("添加失败");window.location.href="/admin/lunbo/lunbo/add"</script>';
		}
	}
	public static function uploadPic($savePath,$rootPath){
		$upload=new \Think\Upload();
		$upload->maxsize=3145728;
		$upload->exts=array('jpg','png','gif','jpeg');
		$upload->savePath=$savePath;
		$upload->rootPath=$rootPath;
		$info=$upload->upload();
		// if(!$info){
		// 	$this->error($upload->getError());
		// }
		return ltrim($upload->rootPath,'.').$info['pic']['savepath'].$info['pic']['savename'];
	}


	public function Blog(){
		$m=M('lunbo');
		$data = $m->where('auth=1')->select();
		$this->assign('data',$data);
		$this->display('/lunbo/Blog/index');
	}

	public function Bupdate(){
		// var_dump(I('post.'));
		$m=M('lunbo');
		$data['status']=I('post.status');
		$res=$m->where('id='.I('post.id'))->save($data);
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}

	public function Bdel(){
		// var_dump(I('post.'));
		$m=M('lunbo');
		$res=$m->where('id='.I('post.id'))->delete();
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}

	public function shangcheng(){
		$m=M('lunbo');
		$data = $m->where('auth=0')->select();
		$this->assign('data',$data);
		$this->display('/lunbo/shangcheng/index');
	}


	public function Supdate(){
		// var_dump(I('post.'));
		$m=M('lunbo');
		$data['status']=I('post.status');
		$res=$m->where('id='.I('post.id'))->save($data);
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}

	public function Sdel(){
		// var_dump(I('post.'));
		$m=M('lunbo');
		$res=$m->where('id='.I('post.id'))->delete();
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
}
?>